/*
 ------------------------------------------------------------------------------
 Copyright (C) 2006-2007 Team Blur.

 This file is part of the Quantum Engine source code.

 The Quantum Engine source code is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or (at your
 option) any later version.

 The Quantum Engine source code is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 more details.

 You should have received a copy of the GNU General Public License along with
 the Quantum Engine source code; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 ------------------------------------------------------------------------------
*/


// common.h -- common definitions between client and server


#ifndef __COMMON_H__
#define __COMMON_H__

#include "../interface/engine_interface.h"
#include "../gamelib/gamelib.h"
#include "core_memory.h"
#include "core_fileSystem.h"
#include "core_cmdSystem.h"
#include "core_cvarSystem.h"
#include "core_protocol.h"
#include "core_parser.h"
#include "core_system.h"
#include "core_formats.h"

#define MAX_MSGLEN			16384
#define MAX_PRINT_MESSAGE			4096		// Max length of a message printed to the console

#define ENGINE_NAME					"Quantum"
#define ENGINE_VERSION				"Quantum Engine 0.60"

#define BASE_DIRECTORY				"base"

#define DEFAULT_ACTION				"cinematic blur.RoQ\n"

#define PORT_SERVER				27510
extern cvar_t *		com_version;
extern cvar_t *		com_dedicated;
extern cvar_t *		com_developer;
extern cvar_t *		com_fixedTime;
extern cvar_t *		com_timeScale;
extern cvar_t *		com_viewLog;
extern cvar_t *		com_speeds;
extern cvar_t *		com_maxFPS;
extern cvar_t *		com_logFile;

extern int			com_frameTime;
extern int			com_frameMsec;
extern int			com_frameCount;

// com_speeds times
extern int			com_timeAll, com_timeWaiting;
extern int			com_timeServer, com_timeClient;
extern int			com_timeFrontEnd, com_timeBackEnd, com_timeSound;

// Begins redirection of console output to the given buffer
void		Com_BeginRedirect (char *buffer, int size, void (*flush)(const char *));

// Ends redirection of console output
void		Com_EndRedirect (void);

// Prints a message to the console, and possibly to the log file
void 		Com_Printf (const char *fmt, ...);

// Like Com_Printf, but only prints the message in developer mode
void 		Com_DPrintf (const char *fmt, ...);

// Throws an error and aborts the current frame. If fatal is true, it will quit
// to the system console and wait for the user to terminate the program.
void 		Com_Error (bool fatal, const char *fmt, ...);

// Sets the current server state
void		Com_SetServerState (bool serverState);

// Searches for command line parameters that are "set" commands.
// If match is not NULL, only that variable will be looked for.
// If once is true, the command will be removed. This is to keep it from being
// executed multiple times.
void		Com_StartupVariable (const char *match, bool once);

// Runs a complete frame.
// Called from the program's main loop.
void		Com_Frame (void);

// Initializes all of the engine subsystems.
// Note that currently this can be done only once!
void		Com_Init (char *commandLine);

// Shuts down all of the engine subsystems.
// Note that currently this can be done only once!
void		Com_Shutdown (void);

/*
 ==============================================================================

 KEYS

 ==============================================================================
*/

// These are the key numbers that should be passed to Key_Event.
// Normal keys should be passed as lowercased ASCII.
enum {
	K_TAB					= 9,
	K_ENTER					= 13,
	K_ESCAPE				= 27,
	K_SPACE					= 32,
	K_BACKSPACE				= 127,

	K_UPARROW,
	K_DOWNARROW,
	K_LEFTARROW,
	K_RIGHTARROW,

	K_INS,
	K_DEL,
	K_HOME,
	K_END,
	K_PGUP,
	K_PGDN,

	K_LALT,
	K_RALT,
	K_LCTRL,
	K_RCTRL,
	K_LSHIFT,
	K_RSHIFT,

	K_F1,
	K_F2,
	K_F3,
	K_F4,
	K_F5,
	K_F6,
	K_F7,
	K_F8,
	K_F9,
	K_F10,
	K_F11,
	K_F12,

	K_CAPSLOCK,
	K_SCROLLLOCK,
	K_PAUSE,

	K_APPS,
	K_LWIN,
	K_RWIN,

	K_KP_HOME,
	K_KP_UPARROW,
	K_KP_PGUP,
	K_KP_LEFTARROW,
	K_KP_5,
	K_KP_RIGHTARROW,
	K_KP_END,
	K_KP_DOWNARROW,
	K_KP_PGDN,
	K_KP_INS,
	K_KP_DEL,
	K_KP_SLASH,
	K_KP_STAR,
	K_KP_MINUS,
	K_KP_PLUS,
	K_KP_ENTER,
	K_KP_NUMLOCK,

	// Mouse buttons and wheel generate virtual keys
	K_MOUSE1,
	K_MOUSE2,
	K_MOUSE3,
	K_MOUSE4,
	K_MOUSE5,

	K_MWHEELUP,
	K_MWHEELDOWN
};

// In order from highest priority to lowest.
// If none of the catchers are active, bound key strings will be executed.
typedef enum {
	KEYCATCH_CONSOLE		= BIT(0),
	KEYCATCH_GUI			= BIT(1)
} keyCatch_t;


// Returns a key number to be used to index a key binding by looking at the
// given string. Single ASCII characters return themselves, while the K_* names
// are matched up.
int					Key_StringToKeyNum (const char *string);

// Returns a string (either a single ASCII char, or a K_* name) for the given
// key number
const char *		Key_KeyNumToString (int keyNum);

// Gets a key binding
const char *		Key_GetBinding (int keyNum);

// Sets a key binding
void				Key_SetBinding (int keyNum, const char *binding);

// Writes lines containing "bind key command" to the given file for all the
// active key bindings.
// Used for config files.
void				Key_WriteBindings (fileHandle_t f);

// Gets the current key catchers
int					Key_GetCatchers (void);

// Sets the current key catchers
void				Key_SetCatchers (int catchers);

// Removes the given key catchers
void				Key_RemoveCatchers (int catchers);

// Sets edit-mode on/off.
// This is for use by integrated editors.
void				Key_SetEditMode (bool editMode);

// Returns true if the given key is down
bool				Key_IsDown (int key);

// Clears key states
void				Key_ClearStates (void);

// Called by the system for both key down and key up events
void				Key_Event (int key, bool down, int time);

// Called by the system for char events
void				Key_CharEvent (int c);

// Initializes the key binding commands
void				Key_Init (void);

// Shuts down the key binding commands
void				Key_Shutdown (void);

/*
 ==============================================================================

 CLIENT / SERVER SYSTEMS

 ==============================================================================
*/

void		CM_ClearDynamicModelInstances (void);
void		CM_PrintStats (void);
void		CM_Init (void);
void		CM_Shutdown (void);

void		Con_Print (const char *text);

void		Key_WriteBindings (fileHandle_t f);
void		Key_Init (void);
void		Key_Shutdown (void);

void		CL_Drop (const char *message);
void		CL_LevelLoading (void);
void		CL_UpdateScreen (void);
void		CL_ForwardCommandToServer (void);
void		CL_InitAll (void);
void		CL_ShutdownAll (void);

void		CL_Frame (int msec);
void		CL_Init (void);
void		CL_Shutdown (void);

void		SV_Frame (int msec);
void		SV_Init (void);
void		SV_Shutdown (const char *message);


#endif	// __COMMON_H__
